package com.gzhryc.shared_device.oem.code.member.services;

import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.base.models.OperateInfo;
import com.gzhryc.shared_device.oem.code.member.dao.MemberWalletOperateDao;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberWalletOperate;
import com.gzhryc.shared_device.oem.code.member.services.dto.MemberWalletOperateSearch;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class MemberWalletOperateService {

	static Logger log = Logger.getLogger(MemberWalletOperateService.class);

	public static MemberWalletOperateService self(Date date) {
		return MultiDBTools.getService(MemberWalletOperateService.class, date);
	}

	public static MemberWalletOperateService self(String jdbcKey,Date date) {
		return MultiDBTools.getService(jdbcKey, MemberWalletOperateService.class, date);
	}

	MemberWalletOperateDao dao;

	public MemberWalletOperateService(String jdbcKey,Date date) {
		dao = new MemberWalletOperateDao(jdbcKey,date);
	}

	public List<MemberWalletOperate> findBySearch(MemberWalletOperateSearch search, Integer pageNo, Integer pageLimit){
		Conditions condition = dao.buildConditions(search);
		condition.desc("createDate");
		try {
			return dao.findByCondition(condition,pageNo,pageLimit);
		} catch (SQLException e) {
			log.error(e.getMessage(),e);
		}
		return null;
	}

	public Long countBySearch(MemberWalletOperateSearch search){
		Conditions condition = dao.buildConditions(search);
		try {
			return dao.count(condition);
		} catch (SQLException e) {
			log.error(e.getMessage(),e);
		}
		return 0L;
	}

	public boolean addOperate(Long memberId, Integer moneyType, Integer type, Long money, OperateInfo operateInfo) {
		MemberWalletOperate operate = new MemberWalletOperate();
		operate.setMemberId(memberId);;
		operate.setMoneyType(moneyType);
		operate.setType(type);
		operate.setMoney(money);
		operate.setOperator(operateInfo.getOperator());
		operate.setOperatorType(operateInfo.getOperatorType());
		operate.setOperatorTypeName(operateInfo.getOperatorTypeName());
		operate.setOperateNote(operateInfo.getOperateNote());
		operate.setCreateDate(new Date());
		try {
			return dao.insert(operate) > 0;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean deleteByMemberId(Long memberId){
		if(memberId != null){
			Conditions conditions = new Conditions(MemberWalletOperate.class);
			conditions.and().is("memberId",memberId);

			try {
				return dao.deleteByCondition(conditions) >= 0;
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}
		}
		return false;
	}
}
